import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/11
 * Time: 8:56
 * Description:
 */
public class Test {

    /**
     * 时间复杂度：
     * 最坏情况下：O(n^2)【逆序的时候】
     * 最好情况下：O(n)【越有序越快】
     * 空间复杂度：O(1)
     *
     * 稳定性：稳定的排序
     * 【一个稳定的排序，可以实现为不稳定的排序。
     *  但是本身就是一个不稳定的排序，你是不可能实现为一个稳定的排序的】
     *
     * 技巧：如何快速判断一个排序是否稳定？ 就看是否有发生跳跃式的交换
     *
     * 场景：同学，我有一组数据，数量不太多，可以认为是常数，而且，这些数据已近大部分有序了或者趋近于有序了。
     * ：O(n)
     * @param array
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 时间复杂度：O(n^1.3 - n^1.5)
     * 空间复杂度：O(1)
     * 稳定性：不稳定的排序
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            shell(array, gap);
            gap = (gap / 3) + 1; // OR gap = gap / 2;
        }
        shell(array, 1);//保证最后增量是1
    }

    public static void shellSort1(int[] array) {
        int[] drr = {5,3,1};//增量序列，这个不好求。 有1W个数据 也是5 3 1 吗？？ 肯定不是
        for (int i = 0; i < drr.length; i++) {
            shell(array,drr[i]);
        }
    }

    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    /**
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定的排序
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] > array[j]) {
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }


    public static void shiftDown(int[] array,int parent,int len) {
        int child = (2*parent)+1;
        while (child < len) {
            if(child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                int tmp = array[child];
                array[child] = array[parent];
                array[parent] = tmp;
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

    public static void createHeap(int[] array) {
        for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
            shiftDown(array,i,array.length);
        }
    }

    /**
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void heapSort(int[] array) {
        //大根堆 O(N)
        createHeap(array);

        int end = array.length-1;
        while (end > 0) {
            int tmp = array[end];
            array[end] = array[0];
            array[0] = tmp;
            shiftDown(array,0,end);
            end--;
        }
    }


    /**
     * 都是不优化冒泡情况下：
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定的排序
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if(!flg) {
                break;
            }
        }
    }

    public static int partition(int[] array,int start,int end) {
        int tmp = array[start];
        while (start < end) {
            while (start < end && array[end] >= tmp) {
                end--;
            }
            array[start] = array[end];
            while (start < end && array[start] <= tmp) {
                start++;
            }
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }

    public static void swap(int[] array,int left,int right) {
        int tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
    }

    public static void mid_three(int[] array,int left,int right) {
        int mid = (left+right) / 2;
        //array[mid] <= array[left] <= array[right]
        if(array[left] > array[right]) {
            swap(array,left,right);
        }//array[left] <= array[right]

        if(array[left] < array[mid]) {
            swap(array,left,mid);
        }//array[left] >= array[mid]

        if(array[mid] > array[right]) {
            swap(array,mid,right);
        }//array[right] >= array[mid]

    }
    public static void insertSort2(int[] array,int low,int high) {
        for (int i = low+1; i <= high; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= low; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public static void quick(int[] array,int low,int high) {
        if(low >= high) {
            return;
        }
        //2、插入排序
        if((high-low+1) <= 48) {
            //直接插入排序
            insertSort2(array,low,high);
            return;
        }

        //1、三数取中法 进行优化
        mid_three(array,low,high);

        int pivot = partition(array,low,high);

        quick(array,low,pivot-1);
        quick(array,pivot+1,high);
    }


    /**
     * 时间复杂度：O(N*logn)
     * 最好 ：O(N*logn). 每次能够均匀的分割待排序序列
     * 最坏：O(N^2)   有序
     * 空间复杂度：
     * 最好 ：logn
     * 最坏：O(n)
     * 稳定性：不稳定的排序
     * @param array
     */
    public static void quickSort1(int[] array) {
        quick(array,0,array.length-1);
    }

    public static void quickSort(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int low = 0;
        int high = array.length-1;
        int pivot = partition(array,low,high);
        if(pivot > low+1) {
            stack.push(low);
            stack.push(pivot-1);
        }
        if(pivot < high-1) {
            stack.push(pivot+1);
            stack.push(high);
        }
        while (!stack.empty()) {
            high = stack.pop();
            low = stack.pop();
            pivot = partition(array,low,high);
            if(pivot > low+1) {
                stack.push(low);
                stack.push(pivot-1);
            }
            if(pivot < high-1) {
                stack.push(pivot+1);
                stack.push(high);
            }
        }
    }

    public static void merge(int[] array,int low,int mid,int high) {
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        int[] tmpArr = new int[high-low+1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            //不加等于号  就不是稳定的排序
            if(array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        //写回数据到原来的数组
        for (int i = 0; i < k; i++) {
            array[i+low] = tmpArr[i];
        }
    }

    public static void mergeSortIn(int[] array,int low,int high) {
        if(low >= high) {
            return;
        }
        int mid = (low+high) / 2;
        mergeSortIn(array,low,mid);
        mergeSortIn(array,mid+1,high);
        merge(array,low,mid,high);
    }

    /**
     * 时间复杂度：O(n*logn)
     * 空间复杂度：O(n)
     * 稳定性：稳定的排序
     * @param array
     */
    public static void mergeSort(int[] array) {
        mergeSortIn(array,0,array.length-1);
    }

    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i+gap*2) {
                int low = i;
                int mid = low+gap-1;
                int high = mid+gap;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(high >= array.length) {
                    high = array.length-1;
                }
                merge(array,low,mid,high);
            }
            gap = gap*2;
        }
    }

    public static void main2(String[] args) {
        int[] array = new int[10_0000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(1000);
            //array[i] = i;
        }
        long start = System.currentTimeMillis();
        quickSort(array);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    public static void main(String[] args) {
        int[] array = {12,5,9,34,6};
        System.out.println(Arrays.toString(array));
        mergeSortNor(array);
        System.out.println(Arrays.toString(array));
    }

}
